package com.zyk.leetcode;

import java.util.*;

/**
 * @author zhangsan
 * @date 2021/4/26 16:54
 */
public class C20 {


    public static boolean isValid(String s) {
        char[] str = s.toCharArray();
        Stack<Character> stack = new Stack<>();

        Map<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');

        stack.add(str[0]);
        for (int i = 1; i < str.length; i++) {
            if(!stack.isEmpty() && stack.peek() == map.get(str[i])) {
                stack.pop();
            }else {
                if(map.containsKey(str[i])) return false;   // 如果这里是闭合括号就开始错了
                stack.add(str[i]);
            }
        }
        return stack.isEmpty();
    }


    // for test
    public static void main(String[] args) {
        //"([)]"
        //"{[]}"
        String s = "([)]";
        System.out.println(isValid(s));
    }

}
